﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autenticazione.DB;
using System.Data.SqlClient;
using System.Data;

namespace Autenticazione.BusinessObject
{
    public class Coach
    {
        public string EMail;
        public long Line;
        public string FullName;
        public string Phone;

        public Coach()
        {
        }

        public Coach(string email, long line, string fullName, string phone)
        {
            this.EMail = email;
            this.Line = line;
            this.FullName = fullName.Replace("'", "''");
            this.Phone = phone;
        }

        public DataTable GetTbCoaches(string email)
        {
            string strSQL = string.Format(@"select * from Allenatore where Email = '{0}' order by FullName", email);
            DB.DB db = new DB.DB();

            return db.GetTable(strSQL);

        }

        public string Serialize()
        {
            string strSQL = string.Format(@"select * from Allenatore order by FullName");
            DB.DB db = new DB.DB();
            DataTable dt = db.GetTable(strSQL);

            dt.TableName = "Allenatore";

            System.IO.StringWriter writer = new System.IO.StringWriter();

            dt.WriteXml(writer, XmlWriteMode.IgnoreSchema, true);

            return writer.ToString();
        }

        public List<Coach> GetCoaches(string email)
        {
            SqlDataReader dr = null;
            List<Coach> coaches = new List<Coach>();
            string strSQL = string.Format(@"select * from Allenatore where Email = '{0}' order by FullName", email);
            DB.DB db = new DB.DB();

            dr = db.Reader(strSQL);

            if (dr == null)
                return coaches;

            while (dr.Read())
                coaches.Add(new Coach(dr["Email"].ToString(), long.Parse(dr["Line"].ToString()),
                                dr["FullName"].ToString(), 
                                dr["Phone"].ToString()));

            db.CloseConnection();

            return coaches;
        }

        public bool DeleteCoach()
        {
            string strSQL = string.Format(@"delete from Allenatore where Line = {0}", this.Line.ToString());
            DB.DB db = new DB.DB();
            Boolean deleted = db.ExecuteNonQuery(strSQL);

            return deleted;
        }

        public bool Salva()
        {
            string strSQL = string.Format(@"insert into Allenatore (EMail, FullName, Phone) values ('{0}','{1}', '{2}')", this.EMail, this.FullName, this.Phone);
            DB.DB db = new DB.DB();
            Boolean saved = db.ExecuteNonQuery(strSQL);

            return saved;
        }

        public bool Update()
        {
            string strSQL = string.Format(@"update Allenatore set FullName = '{0}', Phone = '{1}' where Line = {2}", this.FullName, this.Phone, this.Line.ToString());
            DB.DB db = new DB.DB();
            Boolean saved = db.ExecuteNonQuery(strSQL);

            return saved;
        }

    }
}